package leetcode.t2String;

import java.util.ArrayList;
import java.util.List;

/**
 * 6. Z字形变换
 * 使用flag标记移动方向，阈值判断改变方向
 *
 * @author K
 * @date 2024/1/23 14:34
 */
public class T6_ZConvert {

    public static String convert(String s, int numRows) {
        if (numRows <= 1) {
            return s;
        }
        List<List<Character>> list = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            ArrayList<Character> list1 = new ArrayList<>();
            list.add(list1);
        }
        int down = 0;
        //true向下移动，false向右移动
        boolean flag = true;
        for (char c : s.toCharArray()) {
            if (flag) {
                List<Character> characters = list.get(down);
                characters.add(c);
                down++;
                if (down == numRows) {
                    flag = false;
                    down--;
                }
            } else {
                down--;
                List<Character> characters = list.get(down);
                characters.add(c);
                if (down == 0) {
                    flag = true;
                    down++;
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        for (List<Character> characterList : list) {
            for (Character character : characterList) {
                sb.append(character);
            }
        }

        return sb.toString();
    }


    public static void main(String[] args) {
        String result = convert("PAYPALISHIRING", 3);
        String result1 = convert("PAYPALISHIRING", 4);
        System.out.println();
    }

}
